package com.hspedu.tankgame05;

//创建现场->射击子弹
public class Shot implements Runnable{
    //子弹坐标
    int x;
    int y;
    int direct = 0;//子弹方向
    int speed = 2;//子弹速度
    boolean isALive = true;//记录子弹存活状态

    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    @Override
    public void run() {
        //射击
        while (true) {
            //子弹休眠->防止闪现
            //休眠50ms
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //根据方向改变x、y坐标
            switch (direct) {
                case 0://上
                    y -= speed;
                    break;
                case 1://右
                    x += speed;
                    break;
                case 2://下
                    y += speed;
                    break;
                case 3://左
                    x -= speed;
                    break;
            }
            //测试，输出子弹坐标
            System.out.println("子弹：x="+ x + "y=" + y);
            //线程销毁
            //条件：碰到游戏边界或敌人身上
            //增加退出条件：打在敌人上
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750 && isALive)) {
                isALive = false;
                System.out.println("线程结束");
                break;//退出循环
            }
        }
    }
}
